<?php
/**
 * Created by PhpStorm.
 * User: a
 * Date: 2016/6/3
 * Time: 17:23
 */
namespace Admin\Controller\Admin;
use Think\Controller;
use Admin\Controller\Common\BaseController;

class UserController extends BaseController{
    public function user_main(){
        $user_name=I("get.user_name","");
        $user_phone=I("get.user_phone","");
        $role_id=I("get.role_id","");
        $page=I("get.page","1");

        $user=D("Userinfo");
        $user_array=$user->get_user_info($user_name,$user_phone,$role_id);
        $json=json_encode($user_array);
        $role=D("Role");
        $role_array=$role->select_role();
        $this->assign("role_array",$role_array);
        $this->assign('user_json',$json);
        $this->assign('user_name',$user_name);
        $this->assign('user_phone',$user_phone);
        $this->assign('role_id',$role_id);
        $this->assign('page',$page);
        $this->display();
//        $this->show($json);
    }

    public function user_add(){
//        $url=substr($_SERVER["REQUEST_URI"],strpos($_SERVER["REQUEST_URI"],"?"));
        $role=D("Role");
        $role_array=$role->select_role();
//        $role_json=json_encode($role_array);
        $this->assign("role_array",$role_array);
//        $this->assign("url",$url);
        $this->display();
    }


    //新增后台用户
    public function user_add_ajax(){
        $load_name=I("post.load_name","");
        $user_name=I("post.user_name");
        $user_phone=I("post.user_phone");
        $user_role=I("post.user_role");
        $fac_id=I("post.fac_id","");
        $send_message=I("post.send_message","0");


        $password=md5("123456");
        $user_data['user_name']=$user_name;
        $user_data['load_name']=$load_name;
        $user_data['load_pwd']=$password;
        $user_data['user_phone']=$user_phone;

        $user=D("bypal_comm_user");
        $result=$user->data($user_data)->add();//添加用户
        if($result){
            $user_id=$result;
            $role=D("Role");
            $role_data['user_id']=$user_id;
            $role_data['role_id']=$user_role;
            $role->add_user_role($role_data);//添加用户--角色之间关系

            if($send_message==1){
                $sendMessage=new \Admin\Controller\Common\SendMessageController();
                $array=array(
                    "0"=>array("facName",$user_name),
                    "1"=>array("loadName",$load_name)
                );
                $message=$sendMessage->short_message_context("0024",$array);
                $sendMessage->sendSMS("",$user_phone,$message);
            }

            if($fac_id!='') {
                $fac_user = D("bypal_comm_userfac");
                $fac_data['fac_id'] = $fac_id;
                $fac_data['user_id'] = $user_id;
                $fac_user->data($fac_data)->add();//添加用户--商户之间关系
            }
            $ajax_result['state']=1;
        }else{
            $ajax_result['state']=2;
        }
        $this->ajaxReturn(json_encode($ajax_result));
    }
    //验证登录名是否唯一
    public function validate_load_name(){
        $name=I("get.fieldId");
        $value=I("get.fieldValue");
        $user_id=I("get.user_id","");
        $user=D("Userinfo");
        $user_array=$user->get_user_by_name($value,$user_id);
        $count=count($user_array);
        $result[0]=$name;
        if($count!=0){
            $result[1]=false;
        }else{
            $result[1]=true;
        }
        echo json_encode($result);
    }

    //用户编辑
    public function user_edit(){
        $user_id=I("get.user_id");
        $user_name=I("get.user_name","");
        $role_id=I("get.role_id"."");
        $user_phone=I("get.user_phone","");
        $page=I("get.page","1");

        $url="../../Admin/user/user_main?user_name=$user_name&role_id=$role_id&user_phone=$user_phone&page=$page";
        $user=D("Userinfo");
        $user_array=$user->get_user_by_id($user_id);

        $role=D("Role");
        $role_array=$role->select_role();

        $this->assign("role_array",$role_array);
        $this->assign("user_array",$user_array[0]);
        $this->assign("url",$url);
        $this->display();
    }

    public function user_edit_ajax(){
        $load_name=I("post.load_name","");
        $user_name=I("post.user_name");
        $user_phone=I("post.user_phone");
        $user_role=I("post.user_role");
        $fac_id=I("post.fac_id","");
        $fac_id_http=I("post.fac_id_http","");
        $send_message=I("post.send_message","0");
        $user_id=I("post.user_id");

        $user_data['user_name']=$user_name;
        $user_data['load_name']=$load_name;
        $user_data['user_phone']=$user_phone;

        $user=D("bypal_comm_user");
        $result=$user->where("user_id=$user_id")->save($user_data);//添加用户
            $role=D("bypal_comm_userrole");
            $role_data['role_id']=$user_role;
            $role_data['user_id']=$user_id;
            $role->where("user_id=$user_id")->delete();
            $role->data($role_data)->add();//添加用户--角色之间关系

            if($send_message==1){
                $sendMessage=new \Admin\Controller\Common\SendMessageController();
                $array=array(
                    "0"=>array("facName",$user_name),
                    "1"=>array("loadName",$load_name)
                );
                $message=$sendMessage->short_message_context("0024",$array);
                $sendMessage->sendSMS("",$user_phone,$message);
            }

            $ajax_result['fac_id']=$fac_id;
            $ajax_result['fac_id_http']=$fac_id_http;
            if($fac_id!=''&&$fac_id_http!='') {
                $fac_user = D("bypal_comm_userfac");
                $fac_data['fac_id'] = $fac_id;
                $fac_user->where("user_id=$user_id")->save($fac_data);//添加用户--商户之间关系
            }else if($fac_id!=''&&$fac_id_http==''){
                $fac_user = D("bypal_comm_userfac");
                $fac_data['fac_id'] = $fac_id;
                $fac_data['user_id']=$user_id;
                $fac_user->data($fac_data)->add();//添加用户--商户之间关系
            }
        $ajax_result['state']=1;
        $this->ajaxReturn(json_encode($ajax_result));
    }
}